<?php

namespace app\controller\api;

use app\exception\ModelEmptyException;
use app\exception\ModelException;
use app\model\Category;
use app\model\SubContent;
use app\service\ContentService;
use think\db\exception\DbException;

class ContentController extends BaseController
{

    /**
     * 获取内容列表
     * */
    public function list(ContentService $contentService): \think\Response
    {
        $module_id = (int)input('param.module_id');
//        $categoryIds = input('param.category_id');
        $param = [
            'seller_id' => '1', //只有一个站写死
            'limit' => 8,//暂时3个
            'lang' => input('param.lang'),
            'is_del' => 1,
            'module_id' => $module_id,
            'website_id' => 1,
//            'category_ids' => $categoryIds
        ];

        $SubContent = new SubContent();

        try {
            $res = $SubContent->where(['seller_id' => 1, 'is_del' => 1, 'lang' => $param['lang'], 'module_id' => $param['module_id'], 'status' => 1, 'check_status' => 3])->with(
                [
                    'thumbnail' => function ($q) {
                        $q->field('id,name,url,type,description');
                    }
                ]
            )->limit($param['limit'])->order('id', 'desc')->select()->toArray();
        } catch (ModelEmptyException|ModelException|DbException $e) {
            return jsonReturn(50013, $e->getMessage());
        }
        return jsonReturn(0, 'success', $res);

    }

    /**
     * 获取内容详情
     * */
    public function detail(ContentService $contentService): \think\Response
    {
        $param = request()->only(['id', 'module_id', 'lang', 'seller_id']);

        try {
            $res = $contentService->readContent($param);
        } catch (ModelEmptyException|ModelException $e) {
            return jsonReturn(50025, $e->getMessage());
        }
        return $res;
    }

    /**
     * 获取文章推荐
     * */
    public function getHomeNews(ContentService $contentService): \think\Response
    {
        $param = request()->only(['id', 'module_id', 'lang']);
        $SubContent = new SubContent();
        $data = [];
//      todo  改分类id,就能搜出对应的 is_top
        try {
            $res = $SubContent->where(['seller_id' => 1,'is_top'=>1, 'category_id'=> 9, 'is_del' => 1, 'lang' => $param['lang'], 'module_id' => $param['module_id'], 'status' => 1, 'check_status' => 3,])->with(
                [
                    'thumbnail' => function ($q) {
                        $q->field('id,name,url,type,description');
                    }
                ]
            )->limit(3)->order('id', 'desc')->select()->toArray();
            $res2 = $SubContent->where(['seller_id' => 1,'is_top'=>1, 'category_id'=> 6, 'is_del' => 1, 'lang' => $param['lang'], 'module_id' => $param['module_id'], 'status' => 1, 'check_status' => 3,])->with(
                [
                    'thumbnail' => function ($q) {
                        $q->field('id,name,url,type,description');
                    }
                ]
            )->limit(4)->order('id', 'desc')->select()->toArray();
            $res3 = $SubContent->where(['seller_id' => 1,'is_top'=>1, 'category_id'=> 5, 'is_del' => 1, 'lang' => $param['lang'], 'module_id' => $param['module_id'], 'status' => 1, 'check_status' => 3,])->with(
                [
                    'thumbnail' => function ($q) {
                        $q->field('id,name,url,type,description');
                    }
                ]
            )->limit(3)->order('id', 'desc')->select()->toArray();
            $data['one'] = $res;
            $data['two'] = $res2;
            $data['three'] = $res3;

        } catch (ModelEmptyException|ModelException|DbException $e) {
            return jsonReturn(50013, $e->getMessage());
        }
        return jsonReturn(0, 'success', $data);
    }

    /**
     * 获取首页分类还有产品
     *
     * return list[
     *   [category
     *      children: []
     * ]
     * ]
     * */
    public function homeProduct(Category $category, ContentService $contentService): \think\Response
    {
        $param = request()->only(['lang']);
//        产品分类
        $with = [
            'thumbnail' => function ($query) {
                $query->field('id,name,url');
            }
        ];
        $where[] = ['lang', '=', $param['lang']];
        $where[] = ['status', '=', 1];
        $where[] = ['module_id', '=', 3];

        $categoryList = $category->getAllCustomArrayData($where, 'id asc', 'id,parent_id,title,lang,thumbnail,status', $with)['data'];
        $category_ids = array_column($categoryList, 'id');
        $res = $contentService->getList($category_ids, $param);

        foreach ($categoryList as $k => $v) {
            foreach ($res as $n => $m) {
                if ($v['id'] == $m['category_id']) {
                    $categoryList[$k]['children'][] = $m;
                }
            }
        }
        $arr = array_column($categoryList, 'parent_id');
        if (count($arr) > count(array_unique($arr))) {
            $categoryList = $this->huidiao($categoryList);
        }

        foreach ($categoryList as $k => $v) {
            if ($v['parent_id'] != 0) {
                unset($categoryList[$k]);
            }
        }

        return jsonReturn(0, 'success', $categoryList);
    }

    public function huidiao($categoryList)
    {
        foreach ($categoryList as $k => $v) {
            if ($v['parent_id'] != 0) {
                if (isset($v['children'])) {
                    foreach ($categoryList as $c => $d) {
                        if ($d['id'] == $v['parent_id']) {
                            if (isset($categoryList[$c]['children'])) {
                                foreach ($v['children'] as $s => $z) {
                                    $categoryList[$c]['children'][] = $z;
                                }
                            } else {
                                $categoryList[$c]['children'] = $v['children'];
                            }
                            unset($categoryList[$k]);
                            break;
                        }
                    }
                }
            }
        }
        return $categoryList;
    }


}
